import { API } from './axios';

export const getCurrentCity = () => {
  // 判断本地是否有城市
  const localCity = JSON.parse(localStorage.getItem('hkzf_city'));

  if (!localCity) {
    // Promise 处理异步操作
    return new Promise((resolve, reject) => {
      // 通过IP定位获得当前城市名称
      const currentCity = new window.BMap.LocalCity();
      currentCity.get(async res => {
        try {
          // console.log('当前城市信息：', res);
          const { data } = await API.get(`/area/info?name=${currentCity}`);
          // 存储到本地
          localStorage.setItem('hkzf_city', JSON.stringify(data.body));
          resolve(data.body);

        } catch (e) {
          reject(e);
        }
      });
    });
  }

  return Promise.resolve(localCity);
}

// 导出全部登录权限方法
export * from './auth';
// 导出全部操作城市名方法
export * from './city';